/*   1:    */ package nl.siegmann.epublib.chm;
/*   2:    */ 
/*   3:    */ import java.io.IOException;
/*   4:    */ import java.io.InputStream;
/*   5:    */ import java.util.List;
/*   6:    */ import javax.xml.parsers.ParserConfigurationException;
/*   7:    */ import javax.xml.xpath.XPathExpressionException;
/*   8:    */ import nl.siegmann.epublib.domain.Book;
/*   9:    */ import nl.siegmann.epublib.domain.MediaType;
/*  10:    */ import nl.siegmann.epublib.domain.Metadata;
/*  11:    */ import nl.siegmann.epublib.domain.Resource;
/*  12:    */ import nl.siegmann.epublib.domain.Resources;
/*  13:    */ import nl.siegmann.epublib.domain.TOCReference;
/*  14:    */ import nl.siegmann.epublib.domain.TableOfContents;
/*  15:    */ import nl.siegmann.epublib.service.MediatypeService;
/*  16:    */ import nl.siegmann.epublib.util.ResourceUtil;
/*  17:    */ import org.apache.commons.io.IOUtils;
/*  18:    */ import org.apache.commons.vfs.AllFileSelector;
/*  19:    */ import org.apache.commons.vfs.FileContent;
/*  20:    */ import org.apache.commons.vfs.FileName;
/*  21:    */ import org.apache.commons.vfs.FileObject;
/*  22:    */ import org.apache.commons.vfs.FileSystemException;
/*  23:    */ import org.apache.commons.vfs.FileType;
/*  24:    */ 
/*  25:    */ public class ChmParser
/*  26:    */ {
/*  27:    */   public static final String DEFAULT_CHM_HTML_INPUT_ENCODING = "windows-1252";
/*  28:    */   public static final int MINIMAL_SYSTEM_TITLE_LENGTH = 4;
/*  29:    */   
/*  30:    */   public static Book parseChm(FileObject chmRootDir)
/*  31:    */     throws XPathExpressionException, IOException, ParserConfigurationException
/*  32:    */   {
/*  33: 39 */     return parseChm(chmRootDir, "windows-1252");
/*  34:    */   }
/*  35:    */   
/*  36:    */   public static Book parseChm(FileObject chmRootDir, String inputHtmlEncoding)
/*  37:    */     throws IOException, ParserConfigurationException, XPathExpressionException
/*  38:    */   {
/*  39: 45 */     Book result = new Book();
/*  40: 46 */     result.getMetadata().addTitle(findTitle(chmRootDir));
/*  41: 47 */     FileObject hhcFileObject = findHhcFileObject(chmRootDir);
/*  42: 48 */     if (hhcFileObject == null) {
/*  43: 49 */       throw new IllegalArgumentException("No index file found in directory " + chmRootDir + ". (Looked for file ending with extension '.hhc'");
/*  44:    */     }
/*  45: 51 */     if (inputHtmlEncoding == null) {
/*  46: 52 */       inputHtmlEncoding = "windows-1252";
/*  47:    */     }
/*  48: 54 */     Resources resources = findResources(chmRootDir, inputHtmlEncoding);
/*  49: 55 */     List<TOCReference> tocReferences = HHCParser.parseHhc(hhcFileObject.getContent().getInputStream(), resources);
/*  50: 56 */     result.setTableOfContents(new TableOfContents(tocReferences));
/*  51: 57 */     result.setResources(resources);
/*  52: 58 */     result.generateSpineFromTableOfContents();
/*  53: 59 */     return result;
/*  54:    */   }
/*  55:    */   
/*  56:    */   protected static String findTitle(FileObject chmRootDir)
/*  57:    */     throws IOException
/*  58:    */   {
/*  59: 72 */     FileObject systemFileObject = chmRootDir.resolveFile("#SYSTEM");
/*  60: 73 */     InputStream in = systemFileObject.getContent().getInputStream();
/*  61: 74 */     boolean inText = false;
/*  62: 75 */     int lineCounter = 0;
/*  63: 76 */     StringBuilder line = new StringBuilder();
/*  64: 77 */     for (int c = in.read(); c >= 0; c = in.read()) {
/*  65: 78 */       if ((c >= 32) && (c <= 126))
/*  66:    */       {
/*  67: 79 */         line.append((char)c);
/*  68: 80 */         inText = true;
/*  69:    */       }
/*  70:    */       else
/*  71:    */       {
/*  72: 82 */         if (inText)
/*  73:    */         {
/*  74: 83 */           if (line.length() >= 3)
/*  75:    */           {
/*  76: 84 */             lineCounter++;
/*  77: 85 */             if (lineCounter >= 4) {
/*  78: 86 */               return line.toString();
/*  79:    */             }
/*  80:    */           }
/*  81: 89 */           line = new StringBuilder();
/*  82:    */         }
/*  83: 91 */         inText = false;
/*  84:    */       }
/*  85:    */     }
/*  86: 94 */     return "<unknown title>";
/*  87:    */   }
/*  88:    */   
/*  89:    */   private static FileObject findHhcFileObject(FileObject chmRootDir)
/*  90:    */     throws FileSystemException
/*  91:    */   {
/*  92: 98 */     FileObject[] files = chmRootDir.getChildren();
/*  93: 99 */     for (int i = 0; i < files.length; i++) {
/*  94:100 */       if ("hhc".equalsIgnoreCase(files[i].getName().getExtension())) {
/*  95:101 */         return files[i];
/*  96:    */       }
/*  97:    */     }
/*  98:104 */     return null;
/*  99:    */   }
/* 100:    */   
/* 101:    */   private static Resources findResources(FileObject rootDir, String inputEncoding)
/* 102:    */     throws IOException
/* 103:    */   {
/* 104:109 */     Resources result = new Resources();
/* 105:110 */     FileObject[] allFiles = rootDir.findFiles(new AllFileSelector());
/* 106:111 */     for (int i = 0; i < allFiles.length; i++)
/* 107:    */     {
/* 108:112 */       FileObject file = allFiles[i];
/* 109:113 */       if (file.getType() != FileType.FOLDER)
/* 110:    */       {
/* 111:116 */         MediaType mediaType = MediatypeService.determineMediaType(file.getName().getBaseName());
/* 112:117 */         if (mediaType != null)
/* 113:    */         {
/* 114:120 */           String href = file.getName().toString().substring(rootDir.getName().toString().length() + 1);
/* 115:121 */           byte[] resourceData = IOUtils.toByteArray(file.getContent().getInputStream());
/* 116:122 */           if ((mediaType == MediatypeService.XHTML) && (!"UTF-8".equalsIgnoreCase(inputEncoding))) {
/* 117:123 */             resourceData = ResourceUtil.recode(inputEncoding, "UTF-8", resourceData);
/* 118:    */           }
/* 119:125 */           Resource fileResource = new Resource(null, resourceData, href, mediaType);
/* 120:126 */           result.add(fileResource);
/* 121:    */         }
/* 122:    */       }
/* 123:    */     }
/* 124:128 */     return result;
/* 125:    */   }
/* 126:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.chm.ChmParser
 * JD-Core Version:    0.7.0.1
 */